package com.lanou.musicbox.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.NetworkRadioAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.NetworkRadio;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class NetworkRadioFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private GridView radioGridView;
    private VolleySingleton volleySingleton;
    private List<NetworkRadio.ResultEntity.ListEntity> data;
    private NetworkRadioAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.network_radio, null);
        radioGridView = (GridView) v.findViewById(R.id.radioGridView);
        radioGridView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        initData();// 定义一个解析数据的方法
        return v;
    }

    private void initData() {
        data = new ArrayList<>();
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.radio.getRecChannel&format=json&page_no=1&page_size=39&cuid=CFF13835158F93825D3F294F014C42BD";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                NetworkRadio l = gson.fromJson(response, NetworkRadio.class);
                data = l.getResult().getList();
                adapter = new NetworkRadioAdapter(data, getActivity());
                radioGridView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}
